<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="流程名称" prop="processName">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入流程名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="业务名称" prop="messageTitle">
        <el-input
          v-model="queryParams.messageTitle"
          placeholder="请输入业务名称"
          clearable
        />
      </el-form-item>
      <el-form-item label="接收时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="defaultTime"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        <!-- v-hasPermi="['workflow:process:todoExport']" -->
        <!-- <el-button
          type="primary"
          plain
          icon="el-icon-download"
          @click="handleExport"
          >导出</el-button
        > -->
        <el-button
          type="primary"
          plain
          :disabled="single"
          icon="el-icon-edit"
          @click="handleProcess()"
          >办理</el-button
        >
      </el-form-item>
    </el-form>
    <el-table
      v-loading="loading"
      :data="todoList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" />
      <el-table-column label="流程名称" prop="procDefName">
        <template #default="scope">
          <span class="text-#1890ff pointer" @click="handleProcess(scope.row)"
            >{{ scope.row.procDefName }}
          </span>
        </template>
      </el-table-column>
      <el-table-column
        label="业务名称"
        prop="messageTitle"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column label="任务节点" prop="taskName" />
      <el-table-column label="流程发起人">
        <template #default="scope">
          <label
            >{{ scope.row.startUserName }}
            <el-tag type="info">{{ scope.row.startDeptName }}</el-tag></label
          >
        </template>
      </el-table-column>
      <el-table-column label="接收时间" prop="createTime" width="180" />
      <!-- v-hasPermi="['workflow:process:approval']" -->
      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            type="primary"
            text
            class="text-#1890ff"
            icon="el-icon-edit-outline"
            @click="handleProcess(scope.row)"
            >办理
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script name="myTodo" setup>
import { useRoute, useRouter } from 'vue-router'
import { listTodoProcess } from '@/api/workflow/process'
const { proxy } = getCurrentInstance()
const router = useRouter()
const defaultTime = ref([
  new Date(2000, 1, 1, 0, 0, 0),
  new Date(2000, 2, 1, 23, 59, 59)
])
const data = reactive({
  // 遮罩层
  loading: false,
  // 选中数组
  ids: [],
  taskId: [],
  // 非单个禁用
  single: true,
  // 非多个禁用
  multiple: true,
  // 显示搜索条件
  showSearch: true,
  // 总条数
  total: 0,
  // 流程待办任务表格数据
  todoList: [],
  // 弹出层标题
  title: '',
  // 是否显示弹出层
  open: false,
  // 日期范围
  dateRange: [],
  // 查询参数
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    messageTitle: null,
    category: null
  },
  // 表单参数
  form: {},
  // 表单校验
  rules: {}
})
const {
  loading,
  ids,
  single,
  multiple,
  showSearch,
  total,
  todoList,
  title,
  open,
  dateRange,
  queryParams,
  form,
  taskId,
  rules
} = toRefs(data)
/** 查询流程定义列表 */
function getList() {
  loading.value = true
  listTodoProcess(proxy.addDateRange(queryParams.value, dateRange.value))
    .then(response => {
      todoList.value = response.rows
      total.value = response.total
      loading.value = false
    })
    .catch(e => {
      loading.value = false
    })
}
// 跳转到处理页面
function handleProcess(row) {
  router.push({
    path: '/process/workDetail',
    query: {
      procInsId: row ? row.procInsId : ids.value.toString(),
      taskId: row ? row.taskId : taskId.value.toString(),
      processed: true
    }
  })
}
// 取消按钮
function cancel() {
  open.value = false
  reset()
}
// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    category: null,
    key: null,
    tenantId: null,
    deployTime: null,
    derivedFrom: null,
    derivedFromRoot: null,
    parentDeploymentId: null,
    engineVersion: null
  }
  proxy.resetForm('form')
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm('queryForm')
  handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.procInsId)
  taskId.value = selection.map(item => item.taskId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}
/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加流程定义'
}
/** 导出按钮操作 */
function handleExport() {
  proxy.downLoadByUrl(
    '/bpmn/workflow/process/todoExport',
    {
      ...queryParams.value
    },
    `wf_todo_process_${new Date().getTime()}.xlsx`
  )
}
getList()
</script>
